Skip to content

Method: getPersistenceConnector(String, List, boolean)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.test.environment;
16:
17: import cz.cvut.kbss.jopa.Persistence;
18: import cz.cvut.kbss.jopa.model.EntityManager;
19: import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
20: import cz.cvut.kbss.jopa.model.JOPAPersistenceProvider;
21:
22: import java.io.File;
23: import java.util.*;
24:
25: public class TestEnvironment {
26:
27: public static final String dir = "testResults";
28:
29: public static final String REASONER_FACTORY_CLASS = "com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory";
30: public static final String IRI_BASE = "http://krizik.felk.cvut.cz/ontologies/2013/jopa-tests/";
31:
32: /**
33: * True if the ontology file should be deleted before access to it is initialized. This effectively means that the
34: * test will create the ontology from scratch. Default value is true.
35: */
36: public static final boolean DELETE_ONTOLOGY_FILE = true;
37:
38: // private static final String REASONER_FACTORY_CLASS =
39: // "org.semanticweb.HermiT.Reasoner$ReasonerFactory";
40:
41: /**
42: * Creates persistence connector, with enabled second level cache, for OWLAPI accessed ontology stored in a file.
43: *
44: * @param name Base name used for ontology URI and physical storage path/URI
45: * @return Persistence context
46: */
47: public static EntityManager getPersistenceConnector(String name) {
48: return getOwlapiPersistenceConnector(name, true);
49: }
50:
51: /**
52: * Creates persistence connector for OWLAPI accessed ontology stored in a file.
53: *
54: * @param name Base name used for ontology URI and physical storage path/URI
55: * @param cache Whether second level cache should be enabled
56: * @return Persistence context
57: */
58: public static EntityManager getPersistenceConnector(String name, boolean cache) {
59: return getOwlapiPersistenceConnector(name, cache);
60: }
61:
62: private static EntityManager getOwlapiPersistenceConnector(String name, boolean cache) {
63: return getPersistenceConnector(name,
64: Collections.<StorageConfig>singletonList(new OwlapiStorageConfig()), cache).get(0);
65: }
66:
67: public static EntityManager getPersistenceConnector(String name, StorageConfig storage,
68: boolean cache, Map<String, String> properties) {
69: return getPersistenceConnector(name, Collections.singletonList(storage), cache, properties)
70: .get(0);
71: }
72:
73: /**
74: * Creates persistence connector for the specified list of storages.
75: *
76: * @param baseName Base name used for ontology URI and physical storage path/URI
77: * @param storages List of storage configurations
78: * @param cache Whether second level cache should be enabled
79: * @return Persistence context
80: */
81: public static List<EntityManager> getPersistenceConnector(String baseName,
82: List<StorageConfig> storages, boolean cache) {
83: return getPersistenceConnector(baseName, storages, cache,
84: Collections.<String, String>emptyMap());
85: }
86:
87: /**
88: * Creates persistence connector for the specified list of storages.
89: *
90: * @param baseName Base name used for ontology URI and physical storage path/URI
91: * @param storages List of storage configurations
92: * @param cache Whether second level cache should be enabled
93: * @param props Additional properties for the persistence provider
94: * @return Persistence context
95: */
96: public static List<EntityManager> getPersistenceConnector(String baseName,
97: List<StorageConfig> storages, boolean cache,
98: Map<String, String> props) {
99: final Map<String, String> params = initParams(cache);
100: // Can override default params
101: params.putAll(props);
102: int i = 1;
103: final List<EntityManager> managers = new ArrayList<>(storages.size());
104: for (StorageConfig si : storages) {
105: si.setName(baseName);
106: si.setDirectory(dir);
107: final Map<String, String> config = si.createStorageConfiguration(i++);
108: config.putAll(params);
109: final EntityManager em = Persistence.createEntityManagerFactory("context-name_" + i, config)
110: .createEntityManager();
111: managers.add(em);
112: }
113: return managers;
114: }
115:
116: private static Map<String, String> initParams(boolean cache) {
117: final Map<String, String> params = new HashMap<>();
118: if (cache) {
119: params.put(JOPAPersistenceProperties.CACHE_ENABLED, "true");
120: } else {
121: params.put(JOPAPersistenceProperties.CACHE_ENABLED, "false");
122: }
123: /* Set location of the entities (package) */
124: params.put(JOPAPersistenceProperties.SCAN_PACKAGE, "cz.cvut.kbss.jopa.test");
125: params.put(JOPAPersistenceProperties.JPA_PERSISTENCE_PROVIDER,
126: JOPAPersistenceProvider.class.getName());
127: params.put(JOPAPersistenceProperties.REASONER_FACTORY_CLASS, REASONER_FACTORY_CLASS);
128: return params;
129: }
130:
131: /**
132: * Removes (recursively) the specified file/directory. </p>
133: * <p>
134: * The removal is executed only if the file exists and {@code deleteOntologyFile} is set to {@code true}.
135: *
136: * @param file The file/directory to remove
137: */
138: public static void removeOldTestFiles(File file) {
139: if (file.exists() && DELETE_ONTOLOGY_FILE) {
140: if (file.isDirectory() && file.listFiles() != null) {
141: for (File c : file.listFiles())
142: removeOldTestFiles(c);
143: assert file.delete();
144: } else {
145: if (!file.delete()) {
146: throw new RuntimeException("Unable to delete file " + file);
147: }
148: }
149: }
150: }
151: }